package com.appkubes.mall.restful.application.goods.sku.dao;

import com.appkubes.mall.core.dao.BaseMallGoodsSpecificSkuDao;
import com.appkubes.mall.core.entity.MallGoodsSpecific;
import com.appkubes.mall.restful.application.goods.sku.model.SkuItem;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface GoodsSpecificDao extends BaseMallGoodsSpecificSkuDao {

    @Select("SELECT goods_specific_key, pic, real_stock+virtual_stock as stock, price,origin_price, specific_data FROM mall_goods_specific WHERE goods_key=#{goodsKey} AND specific_template_key=#{specificTemplateKey}")
    List<SkuItem> getSkuItem(@Param("goodsKey") String goodsKey, @Param("specificTemplateKey") String specificTemplateKey);

    @Select("SELECT * FROM mall_goods_specific WHERE goods_key=#{goodsKey} AND specific_template_key=#{templateKey} LIMIT 1")
    MallGoodsSpecific getFirstOne(@Param("goodsKey") String goodsKey, @Param("templateKey") String templateKey);

    @Update({"UPDATE mall_goods_specific SET real_stock=#{stock}, update_time=#{nowTime}  WHERE goods_specific_key=#{specificKey} and update_time=#{goodsTime} and real_stock-#{num}>=0 "})
    int cutStock(@Param("specificKey") String specificKey, @Param("nowTime") int nowTime, @Param("goodsTime") int goodsTime, @Param("stock") Integer stock, @Param("num") Integer num);

    @Update({"UPDATE mall_goods_specific SET real_stock=#{stock}, update_time=#{nowTime}  WHERE goods_specific_key=#{specificKey} and update_time=#{goodsTime}"})
    int updateStock(@Param("specificKey") String specificKey, @Param("nowTime") int nowTime, @Param("goodsTime") int goodsTime, @Param("stock") Integer stock);
}
